package com.example.mywanandroid.ui.nav.todo;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.mywanandroid.base.viewmodel.BaseViewModel;
import com.example.mywanandroid.bean.responsebean.ToDoListBean;
import com.example.mywanandroid.config.LoadState;
import com.example.mywanandroid.http.data.HttpDisposable;
import com.example.mywanandroid.http.request.HttpFactory;
import com.example.mywanandroid.http.request.HttpRequest;
import com.example.mywanandroid.util.CommonUtils;
import com.example.mywanandroid.util.NetworkUtils;

import java.util.ArrayList;
import java.util.List;

public class ToDoViewModel extends BaseViewModel {
    private int mPage;

    private List<ToDoListBean.ToDoData> mList;

    /**
     * 待办清单列表
     */
    private MutableLiveData<ToDoListBean> toDoListBean;

    private MutableLiveData<ToDoListBean.ToDoData> toDoChangeStatus;

    /**
     * 分类（查询条件）
     */
    private MutableLiveData<Integer> queryType;
    /**
     * 级别（查询条件）
     */
    private MutableLiveData<Integer> queryPriority;



    /**
     * 被选中的某一条待办事项
     */
    public MutableLiveData<ToDoListBean.ToDoData> toDoData;

    public ToDoViewModel() {
        toDoListBean = new MutableLiveData<>();
        toDoChangeStatus = new MutableLiveData<>();
        mList = new ArrayList<>();

        queryType = new MutableLiveData<>(0);
        queryPriority = new MutableLiveData<>(0);

        toDoData = new MutableLiveData<>();

    }

    public LiveData<ToDoListBean.ToDoData> getToDoChangeStatus() {
        return toDoChangeStatus;
    }


    /**
     * 提交更新
     */
    public void updateToDoData(ToDoListBean.ToDoData bean) {

        HttpRequest.getInstance()
                .updateToDoList(bean.getId(), bean.getTitle(), bean.getContent(),
                        bean.getDateStr(), bean.getStatus(),
                        bean.getType(), bean.getPriority())
                .compose(HttpFactory.schedulers())
                .subscribe(new HttpDisposable<ToDoListBean.ToDoData>() {
                    @Override
                    public void success(ToDoListBean.ToDoData toDoData) {
                        toDoChangeStatus.postValue(toDoData);
                    }
                });
    }

    /**
     * 获取待办事项列表
     *
     * @return
     */
    public MutableLiveData<ToDoListBean> getToDoListBean() {
        if (toDoListBean == null) {
            toDoListBean = new MutableLiveData<>();
        }
        return toDoListBean;
    }

    public LiveData<ToDoListBean.ToDoData> getData() {
        return toDoData;
    }

    /**
     * 第一次加载数据
     */
    public void loadToDoList() {
        loadState.setValue(LoadState.LOADING);
        mPage = 1;
        mRefresh = false;
        loadData();
    }

    public void refreshData(boolean refresh) {
        if (refresh) {
            mPage = 1;
        } else {
            mPage++;
        }
        loadData();
    }

    public void setQueryType(int i) {
        this.queryType = new MutableLiveData<>(i);
        mPage = 1;
        loadToDoList();
    }

    public void setQueryPriority(int i) {
        this.queryPriority = new MutableLiveData<>(i);
        mPage = 1;
        loadToDoList();
    }

    /**
     * 加载代办数据
     */
    private void loadData() {

        //判断网络
        if (!NetworkUtils.isConnected()) {
            loadState.postValue(LoadState.NO_NETWORK);
            return;
        }

        HttpRequest.getInstance()
                .getToDoList(mPage, queryType.getValue(), queryPriority.getValue())
                .compose(HttpFactory.schedulers())
                .subscribe(new HttpDisposable<ToDoListBean>() {
                    @Override
                    public void success(ToDoListBean bean) {

                        //设置加载状态
                        loadState.setValue(LoadState.SUCCESS);
                        if (mPage == 1) {
                            mList.clear();
                            mList.addAll(bean.getDatas());
                            toDoListBean.postValue(bean);
                        } else {
                            mList.addAll(bean.getDatas());
                            bean.setDatas(mList);
                            toDoListBean.postValue(bean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        errorMsg.postValue(e.getMessage());
                    }
                });
    }

}
